package com.imay.live.manager;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;

import com.imay.live.IMApplication;
import com.imay.live.model.MusicModel;
import com.lcodecore.tkrefreshlayout.utils.LogUtil;

/**
 * Created by Administrator on 2017/7/26.
 */

public class MusicDownLoadManager {

    private DownloadManager downloadManager;
    private DownloadStatuListener downloadStatuListener;
    private static class MusicDownLoadManagerHolder {
        private static final MusicDownLoadManager INSTANCE = new MusicDownLoadManager();
    }

    public static final MusicDownLoadManager getInstance() {
        return MusicDownLoadManager.MusicDownLoadManagerHolder.INSTANCE;
    }

    private MusicDownLoadManager() {
        downloadManager= (DownloadManager) IMApplication.getInstance().getSystemService(Context.DOWNLOAD_SERVICE);
        //注册广播接收者，监听下载状态
        IMApplication.getInstance().registerReceiver(receiver,
                new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    public  long createDownLoadRequset(String musicUrl,String musicName) {
        //创建下载任务
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(musicUrl));


        //在通知栏中显示，默认就是显示的
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
        request.setVisibleInDownloadsUi(false);

        //sdcard的目录下的download文件夹，必须设置
        request.setDestinationInExternalFilesDir(IMApplication.getInstance(), Environment.DIRECTORY_MUSIC,musicName);

        long mTaskId = downloadManager.enqueue(request);
        return mTaskId;

    }

    //广播接受者，接收下载状态
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkDownloadStatus();//检查下载状态
        }
    };

    //检查下载状态
    private void checkDownloadStatus() {
        DownloadManager.Query query = new DownloadManager.Query();
        Cursor c = downloadManager.query(query);
        if (c.moveToFirst()) {
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            long id = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID));
            switch (status) {
                case DownloadManager.STATUS_SUCCESSFUL:
                    if (downloadStatuListener!=null){
                        downloadStatuListener.onDownloadSuccess(id);
                    }
                    break;
                default:
                    if (downloadStatuListener!=null){
                        downloadStatuListener.onDownloadFail(id);
                    }

            }
        }
    }

    public interface DownloadStatuListener{
        void onDownloadSuccess(long id);
        void onDownloadFail(long id);
    }


    public void setDownloadStatuListener(DownloadStatuListener downloadStatuListener) {
        this.downloadStatuListener = downloadStatuListener;
    }

    public void cancle(long id){
        downloadManager.remove(id);
    }
}
